package com.ibeetl.admin.core.conf;

import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.sdx.common.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 描述: 密码工具，系统默认采用明文
 *
 * @author : xiandafu
 */
@Configuration
@ConditionalOnMissingBean(PasswordEncryptService.class)
public class PasswordConfig {
    @Autowired
    Environment env;
	
	public static interface PasswordEncryptService{
		public String password(String pwd,int userId);
	}
	
	
	public static class DefaultEncryptBean implements PasswordEncryptService {

		@Override
		public String password(String pwd,int userId) {
			// 采用明文，系统应该提供自己的EncryptBean实现以代替默认
			return pwd;
		}
		
	}
	public static class Md5EncryptBean implements PasswordEncryptService {
        private String salt;
        public Md5EncryptBean(String salt) {
            this.salt = salt;
        }
		@Override
		public String password(String pwd,int userId) {
			String result = MD5.MD5Encode(userId+salt);
			result = MD5.MD5Encode(result+pwd);
			return result;
		}

	}
	@Bean
	public PasswordEncryptService passwordEncryptBean(){
		return new Md5EncryptBean(env.getProperty("sdx.salt"));
	}
    public static void main(String[] args) {
        Md5EncryptBean md5EncryptBean = new Md5EncryptBean("lka457sgow4ksdig");
	    System.out.println(md5EncryptBean.password("sdxconsole@74cs",1));
    }
}
